package com.cn.kabalifemerchants.ui.user.activity;

import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.BasePresenter;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.ui.login.bean.AddressProvinceListBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.page.bean.UpImgBean;
import com.cn.kabalifemerchants.ui.user.bean.MyUserInfoBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.List;

/**
 * Created by 晓林 on 2020/10/20.
 * function:
 */
public class UserInfoPresenter extends BasePresenter<UserInfoView> {

    /**
     * 我的
     *
     * @param token
     */
    public void onUserInfoData(String token) {
        OkGo.<MyUserInfoBean>get(Api.API_USER_INFO)
                .headers("token", token)
                .execute(new CacheCallBack<MyUserInfoBean>() {
                    @Override
                    public void onSuccess(Response<MyUserInfoBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("我的" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 头像
     *
     * @param token
     * @param head
     */
    public void onUpUserInfoData(String token, String head) {
        OkGo.<MsgBean>post(Api.API_USER_INFO_UP)
                .headers("token", token)
                .params("head", head)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUpUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    /**
     * 上传文件。
     */
    public void onUpImgData(String type, String img) {
        PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
        postRequest.params("type", type);
        postRequest.params("file[]", new File(img));
        postRequest.execute(new CacheCallBack<UpImgBean>(getView()._getContext(), true) {
            @Override
            public void onSuccess(Response<UpImgBean> response) {
                super.onSuccess(response);
                LoggerUtils.d("上传图:" + response.body());
                if (isViewAttached())
                    if (response.body().getCode() == Constants.HTTP_SUCCESS) {
                        getView().onUpImgSuccess(response.body());
                    } else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        getView().onReLoggedIn(response.body().getMsg());
                    } else {
                        getView().onError(response.body().getMsg());
                    }

            }
        });
    }

    /**
     * 退出
     *
     * @param token
     */
    public void onUserOutData(String token) {
        OkGo.<MsgBean>post(Api.API_USER_OUT)
                .headers("token", token)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUserOutSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    public void onShopUpData(String token, String title) {
        OkGo.<MsgBean>post(Api.API_UP_SHOP)
                .headers("token", token)
                .params("title", title)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onShopUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }


    public void onShopUpAddressData(String token, String address) {
        OkGo.<MsgBean>post(Api.API_UP_SHOP)
                .headers("token", token)
                .params("address", address)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onShopUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    public void onShopUpData(String token, int province, int city, int area, int street) {
        OkGo.<MsgBean>post(Api.API_UP_SHOP)
                .headers("token", token)
                .params("province", province)
                .params("city", city)
                .params("area", area)
                .params("street", street)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onShopUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    /**
     * 上传封面
     *
     * @param token
     * @param cover
     */
    public void onShopCoverUpData(String token, String cover) {
        OkGo.<MsgBean>post(Api.API_UP_SHOP)
                .headers("token", token)
                .params("cover", cover)
                .execute(new CacheCallBack<MsgBean>(getView()._getContext(), true) {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onShopUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    /**
     * 店铺修改
     *
     * @param token
     * @param title
     */
    public void onShopUpData(String token, String title, List<String> img, String cover, String info, int province, int city, int area, int street, String address, String service_type) {
        OkGo.<MsgBean>post(Api.API_UP_SHOP)
                .headers("token", token)
                .params("title", title)
                .params("cover", cover)
                .params("info", info)
                .params("province", province)
                .params("city", city)
                .params("area", area)
                .params("street", street)
                .params("address", address)
                .params("service_type", service_type)
                .addUrlParams("img[]", img)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onShopUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }


    //市
    public void onCityListData(int pid) {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", pid)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("市：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCityListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    //省
    public void onProvinceListData() {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", 1)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("省：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onProvinceListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }
}
